home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2299 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.3 KB

  1. Message-ID: <30FBCAA1.34A7@novell.com>
  2. Date: Tue, 16 Jan 1996 15:56:17 +0000
  3. From: Greg Johnson <gregjo@novell.com>
  4. Organization: Novell, Inc.
  5. X-Mailer: Mozilla 2.0b3 (Win95; I)
  6. MIME-Version: 1.0
  7. Newsgroups: comp.lang.c++
  8. Subject: Overload of operator=
  9. Content-Type: text/plain; charset=us-ascii
  10. Content-Transfer-Encoding: 7bit
  11. NNTP-Posting-Host: pcgjohnson2.orem.novell.com
  12. Path: news.provo.novell.com!
  13.  
  14. I want to overload the assignment operator, for some debugging purposes.
  15. But after I overload the operator, somehow, I need to perform the 
  16. default assignment. How do I do that if I don't know the exact size
  17. of the source or destination?
  18. How do I avoid recursion and perform the default behavior?
  19. For example:
  20.  
  21. class BaseObj {
  22. public:
  23.     long x;       
  24.     BaseObj& operator=(BaseObj&);
  25.     BaseObj(void) : x(0) {};
  26. };
  27.  
  28. BaseObj& BaseObj::operator=(BaseObj  & ptr)
  29. {
  30.     *this = ptr; // this will recursively call operator=
  31.     return *this;
  32. }
  33.  
  34. void foo()
  35. {
  36.     BaseObj bo1;
  37.     BaseObj bo2;
  38.  
  39.     bo1.x = 99;
  40.     bo2 = bo1;
  41. }
  42.  
  43. in operator=, the '*this = ptr' line causes a recursive call.
  44. How can I avoid the recursion and perform the correct assignment?
  45. I could do a memcpy, but I need to determine the size of the source
  46. and destination (either one may be base pointers to a more complex 
  47. class.)
  48. Is there a way to determine the size of an object created with 'new'?
  49.